﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace BabyShop.CP
{
    public partial class odetail : System.Web.UI.Page
    {
        public int order_status = 0;
        public int order_id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["action"] == "cstatus" && Request["id"] != "")
            {
                string sql = "update Orders set order_status = ABS(order_status-1) where id=" + Request["id"];
                DB_Provider.Exec_Commnad(sql);
                Response.Redirect("odetail.aspx?id=" + Request["id"]);
            }

            if (!IsPostBack)
            {
                if (Request["id"] != "")
                {
                    int id = Convert.ToInt32(Request["id"]);
                    DataSet ds = DAO.Order.find(id);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataTable orders = ds.Tables["orders"];
                        lbOrderId.InnerText = orders.Rows[0]["ID"].ToString();
                        lbFullname.InnerText = orders.Rows[0]["fullname"].ToString();
                        lbShippingAddress.InnerText = orders.Rows[0]["shipping_address"].ToString();
                        lbShippingName.InnerText = orders.Rows[0]["shipping_name"].ToString();
                        lbShippingTel.InnerText = orders.Rows[0]["shipping_tel"].ToString();
                        lbTotal.InnerText = orders.Rows[0]["total"].ToString();
                        order_status = Convert.ToInt32(orders.Rows[0]["order_status"].ToString());
                        order_id = Convert.ToInt32(orders.Rows[0]["id"].ToString());
                        btn_cstatus.PostBackUrl = "?action=cstatus&id=" + order_id;

                        DataTable order_details = ds.Tables["order_details"];
                        GridView.DataSource = order_details;
                        GridView.Width = 600;
                        GridView.DataBind();
                    }
                    else
                    {
                        Response.Redirect("orders.aspx");
                    }
                }                
            }            
        }

        protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ChangeStatus"))
            {

                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                int index = gvr.RowIndex;
                Label lbID = (Label)GridView.Rows[index].FindControl("lbID");
                Response.Redirect("odetail.aspx?id=" + lbID.Text);
            }
        }

    }
}